package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.common.dto.AttachmentDto;
import com.koron.css2.common.service.AbstractExportExcel;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.serviceManage.vo.BusinessAreaVo;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 工程结算收款余额列表导出
 */
public class PjSettlementPaymentExport extends AbstractExportExcel {

    private static Logger logger = LoggerFactory.getLogger(PjSettlementPaymentExport.class);

    @Override
    public AttachmentDto getAttachment(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        List<Map<String, String>> dataList = new ArrayList<>();
        String filename = "";
        try {
            PjMeterQuery pjMeterQuery = JsonUtils.objectToPojo(req.getData(), PjMeterQuery.class);
            if(StringUtils.isBlank(pjMeterQuery.getConsultId())) {
                throw new IllegalArgumentException("工程报装ID不能为空");
            }
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            pjMeterQuery.setBalanceRefundFlag(1);
            List<PjMeter> pjMeterList = pjMeterMapper.getList(pjMeterQuery);
            PjInfoBean pjInfo = pjInfoMapper.selectById(pjMeterQuery.getConsultId());
            filename = pjInfo.getPjName() + "(" + pjInfo.getBillNo() + ")" + "余额退款用户清单";
            // 获取营业区域
            BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
            List<BusinessAreaVo> businessAreaList = businessAreaMapper.selectList(userInfo.getCurWaterCode());
            Map<String, String> businessMap = businessAreaList.stream().collect(Collectors.toMap(BusinessAreaVo::getId, BusinessAreaVo::getAreaName));

            List<String> accountInfoIdList = pjMeterList.stream().map(t -> t.getUserAccountTempId()).collect(Collectors.toList());
            List<String> userInfoIdList = pjMeterList.stream().map(t -> t.getUserAccountTempId()).collect(Collectors.toList());
            List<String> ctmInfoIdList = pjMeterList.stream().map(t -> t.getUserCtmTempId()).collect(Collectors.toList());
            List<AccountInfoBean> accountInfoList = accountInfoMapper.getListByIdList(accountInfoIdList, AccountInfoBean.ACCOUNTTEMP);
            List<UserBean> userList = userInfoMapper.selectListByIds(userInfoIdList, UserBean.USERTEMP);
            List<CtmInfoBean> ctmInfoList = ctmInfoMapper.selectListByIds(ctmInfoIdList, CtmInfoBean.CTMTEMP);
            Map<String, AccountInfoBean> accountInfoMap = accountInfoList.stream().collect(Collectors.toMap(AccountInfoBean::getId, Function.identity()));
            Map<String, UserBean> userMap = userList.stream().collect(Collectors.toMap(UserBean::getId, Function.identity()));
            Map<String, CtmInfoBean> ctmInfoMap = ctmInfoList.stream().collect(Collectors.toMap(CtmInfoBean::getId, Function.identity()));
            for (PjMeter pjMeter : pjMeterList){
                AccountInfoBean accountInfo = accountInfoMap.get(pjMeter.getUserAccountTempId());
                UserBean user = userMap.get(pjMeter.getUserInfoTempId());
                CtmInfoBean ctmInfo = ctmInfoMap.get(pjMeter.getUserCtmTempId());
                Map<String, String> map = new HashMap<>();
                map.put("ctmNo", Objects.toString(ctmInfo.getCtmNo(), ""));
                map.put("accountNo", Objects.toString(accountInfo.getAccountNo(), ""));
                map.put("userNo", Objects.toString(user.getUserNo(), ""));
                map.put("ctmName", Objects.toString(ctmInfo.getCtmName(), ""));
                map.put("areaName", Objects.toString(businessMap.get(user.getBusinessArea()), " "));
                map.put("setupMeterAddr", Objects.toString(user.getSetupMeterAddr(), ""));
                map.put("setupMeterDate", Objects.toString(user.getSetupMeterDate(), ""));
                if (accountInfo.getAccountBalance() == null){
                    map.put("accountBalance", "0");
                }else {
                    map.put("accountBalance", Objects.toString(accountInfo.getAccountBalance()));
                }
                dataList.add(map);
            }
        } catch (Exception e) {
            logger.error("结算收款列表导出失败", e);
        }
        AttachmentDto attachment = new AttachmentDto();
        attachment.setBtlTemplate("PjSettlementPaymentExport.btl");
        attachment.setData(dataList);
        attachment.setFilename(filename);
        attachment.setFileTemplate("PjSettlementPaymentExport.xlsx");
        attachment.setFileType("excel");
        return attachment;
    }

}
